<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :rules="rules" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="身份证号" prop="queryParams.patientPersonId" >
        <el-input

          v-model="queryParams.patientPersonId"
          placeholder="请输入身份证号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="onePatient">加载身份证号</el-button>
        <el-button icon="el-icon-refresh" size="mini" type="warning" @click="patientLoading">加载患者信息</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <hr/>
    <el-form   ref="form" :model="queryParam" :rules="rules"  :inline="true" v-show="showSearch" label-width="78px" >
      <el-form-item label="身份证" prop="patientPersonId">
        <el-input
          v-model="queryParam.patientPersonId"
          placeholder="请输入身份证号"
        />
      </el-form-item>

      <el-form-item label="患者姓名" prop="patientName">
        <el-input
          v-model="queryParam.patientName"
          placeholder="请输入患者姓名"
        />
      </el-form-item>

      <el-form-item label="患者电话" prop="patientMobile">
        <el-input
          v-model="queryParam.patientMobile"
          placeholder="请输入患者电话"
        />
      </el-form-item>

      <el-form-item label="性别" prop="patientSex">
        <el-radio-group v-model="queryParam.patientSex">
          <el-radio label="0">男</el-radio>
          <el-radio label="1">女</el-radio>
        </el-radio-group>
      </el-form-item><br>

      <el-form-item label="出生日期" prop="patientBirthday">
        <el-date-picker
          v-model="queryParam.patientBirthday"
          type="date"
          placeholder="选择日期">
        </el-date-picker>
      </el-form-item>

      <el-form-item label="患者年龄" prop="patientAge">
        <el-input
          v-model="queryParam.patientAge"
          placeholder="请输入患者年龄"
        />
      </el-form-item>

      <el-form-item label="地址" prop="patientAddress">
        <el-input
          v-model="queryParam.patientAddress"
          placeholder="请输入地址"
        />
      </el-form-item>
    </el-form>
    <hr>
    <el-row>
      <el-button type="primary">门诊</el-button>
      <el-button type="success">门诊+病历本</el-button>
      <el-button type="info">急诊</el-button>
      <el-button type="warning">急诊+病历本</el-button>
      <el-button @click="drawer = true" type="primary" style="margin-left: 1500px" icon="el-icon-search">
        挂号收费
      </el-button>
    </el-row>


<br>
<br>

<br>
<br>
<hr>
    <el-form :model="departmentParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="所属科室" prop="departmentName">
        <el-select v-model="departmentParams.departmentName" placeholder="请选择科室名称" clearable size="small">
          <el-option
            v-for="na in postList"
            :key="na.departmentName"
            :value="na.departmentName"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="挂号类型" prop="registryCostName">
        <el-select v-model="departmentParams.registryCostName" placeholder="请选择挂号类型" clearable size="small">
          <el-option
            v-for="rag in registryCostList"
            :key="rag.registryCostName"
            :value="rag.registryCostName"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="挂号时段" prop="timeFrame">
        <el-select v-model="departmentParams.timeFrame" placeholder="请选择挂号时段" clearable size="small" style="width: 150px">
          <el-option
            v-for="dict in timeFrame"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="挂号时间" prop="postName">
        <el-date-picker
          style="width: 150px"
          v-model="departmentParams.createTime"
          type="date"
          placeholder="选择日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>


    <el-table v-loading="loading" :data="postList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="科室ID" align="center" prop="departmentId" />
      <el-table-column label="科室名称" align="center" prop="departmentName" />
      <el-table-column label="当前号数" align="center" prop="departmentNum" />
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />


    <!-- 添加或修改岗位对话框 -->
    <!--添加表单-->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="患者姓名" prop="patientName">
          <el-input v-model="form.patientName" placeholder="请输入患者姓名" />
        </el-form-item>
        <el-form-item label="身份证号" prop="patientPersonId">
          <el-input v-model="form.patientPersonId" placeholder="请输入身份证号" />
        </el-form-item>
        <el-form-item label="出生日期" prop="patientBirthday">
          <input v-model="form.patientBirthday" type="date" />
        </el-form-item>
        <el-form-item label="年龄" prop="patientBirthday">
          <el-input v-model="form.patientAge"  />
        </el-form-item>
        <el-form-item label="患者电话" prop="patientAge">
          <el-input v-model="form.patientMobile"  />
        </el-form-item>
        <el-form-item label="性别" prop="patientSex">
          <el-radio-group v-model="form.patientSex">
            <el-radio label="男"></el-radio>
            <el-radio label="女"></el-radio>
            <el-radio label="未知"></el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="地址信息" prop="patientAddress">
          <el-input v-model="form.patientAddress" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button  @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listPatient, getPatient, addPatient, addPost, updatePost, exportPost } from "@/api/crud/patient";
import {listdepartment}  from  "@/api/system/department";
import {listregistryCost}  from  "@/api/system/registrycost";


export default {
  name: "Post",
  data() {
    return {
      //性别
      radio: 1,
      flag:0,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
        //上下午时间段
      timeFrame:[],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
        // 科室表格数据
     postList: [],
      // 岗位表格数据
      postList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 状态数据字典
      statusOptions: [],
        //门诊类型
      registryCostList:[],
      // 查询参数
      queryParams: {
        patientPersonId: undefined,
      },queryParam:{
            patientId:undefined,
            patientPersonId:undefined,
            patientName:undefined,
            patientMobile:undefined,
            patientSex:undefined,
            patientBirthday:undefined,
            patientAge:undefined,
            patientAddress:undefined
        },
        //科室
        departmentParams:{
            pageNum: 1,
            pageSize: 10,
            departmentName:undefined,
            departmentId:undefined,
            departmentNum:undefined,
            status: undefined,
            createTime:undefined,
            registryCostName:undefined,
            timeFrame:undefined
        },
      // 表单参数
     form: {
     },
      // 表单校验
      rules: {
        patientName: [
          { required: true, message: "患者姓名不能为空", trigger: "blur" }
        ],
        patientPersonId: [
          { required: true, message: "身份证号不能为空", trigger: "blur" }
        ],
        patientMobile: [
          { required: true, message: "患者电话不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_normal_disable").then(response => {
      this.statusOptions = response.data;
    });
      this.getDicts("time_frame").then(response => {
          this.timeFrame = response.data;
      });
  },
  methods: {
    /** 查询岗位列表 */
    getList() {
      this.loading = true;
        listPatient(this.queryParams).then(response => {
        this.postList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 岗位状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
      /** 搜索按钮操作 */
      handleQuery() {
          this.reset();
          this.queryParams.pageNum = 1;
          this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
          this.resetForm("queryForm");
          this.handleQuery();
      },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
          patientPersonId:undefined,
          patientName:undefined,
          patientMobile:undefined,
          patientSex:undefined,
          patientBirthday:undefined,
          patientAge:undefined,
          patientAddress:undefined
      };
      this.resetForm("form");
    },
      /*加载患者信息*/
      patientLoading(){
          if(this.flag==1){
              this.queryParam =this.load;
              this.msgSuccess("信息加载成功!");
          }else{
              this.msgError("加载失败,请先加载身份证号!")
          }
      },
    /** 搜索按钮操作 */
    onePatient() {
     /* this.queryParams.pageNum = 1;
      this.getList();*/
     const patientPersonId= this.queryParams.patientPersonId;
        getPatient(patientPersonId).then(response => {
            this.reset()
            if (response.data==null){
                this.openMsgNo();
            }else {
                this.load = response.data;
                this.openMsgYes();
            }
        });
    },

      openMsgNo() {

          this.$alert('该身份信息不存在,需要添加 !', '身份信息不存在', {
              confirmButtonText: '确定',
              callback:action=> {
                  this.open = true;
              }
          });
      },
      openMsgYes() {

          this.$alert('身份信息已存在,可以直接加载信息 !', '身份信息已存在', {
              confirmButtonText: '确定',
              callback:action=> {
                  this.flag=1 ;
              }
          });

      },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.postId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加岗位";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const patientId = row.patientId || this.ids
        getPatient(patientId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改岗位";
      });
    },
      /** 提交按钮 */
      submitForm: function() {
          this.$refs["form"].validate(valid => {
              if (valid) {
                  addPatient(this.form).then(response => {
                      this.msgSuccess("新增成功!");
                      this.open = false;
                      this.getList();
                  });
              }
          });
      },

      getList() {
          this.loading = true;
          listdepartment(this.departmentParams).then(response => {
              this.postList = response.rows;
              this.total = response.total;
              this.loading = false;
          });
          listregistryCost(this.departmentParams).then(response => {
              this.registryCostList = response.rows;
              this.total = response.total;
              this.loading = false;
          })
      },
      // 取消按钮
      cancel() {
          this.open = false;
          this.reset();
      },
    /** 删除按钮操作 */
    handleDelete(row) {
      const postIds = row.postId || this.ids;
      this.$confirm('是否确认删除岗位编号为"' + postIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delPost(postIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有岗位数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportPost(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
